﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public EditForm(int id,string name,int age,int score)
        {
              this.id=id;
            this.studentName =name;
            this .age=age;
            this.score=score;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            var abc = "select*from users";
            var stuSql = "select * from students";
            var dt = DbHelper.GetDataTable(stuSql);
            dataGridView1.DataSource = dt;
           
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.ReadOnly = true;
            dataGridView1.AllowUserToAddRows = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var sql = string.Format("select * from StudentName wher StudentName like '%{0}%'");
            var dt = DbHelper.GetDataTable(sql);
            dataGridView1.DataSource = dt;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            EditForm form = new EditForm();
            var res =EditForm.ShowDialog();
            if(res==DialogResult.Yes)
            {
             var var Seachstudent ="select*from StudenInfo";
               var sk =DbHelper.Search(Seachstudent);
                dataGridView1.DataSource=sk;
            }
          else{
            MessageBox.Show("抱歉，您还未添加信息","消息提示框");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var row = dataGridView1.SelectedRows.Count;
            if(row>0)
            {
            var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
            var name = (string)dataGridView1.SelectedRows[0].Cells["StudentName"].Value;
            var age=(int) dataGridView1.SelectedRows[0].Cells["Age"].Value;
            var score =(int) dataGridView1.SelectedRows[0].Cells["Score"].Value;
          
            EditForm form = new EditForm(id,name,age,score);
            //form.Show();
                var res = EditForm.ShowDialog();
            if(res==DialogResult.Yes)
            {
            var Seachstudent ="select*from StudenInfo";
                var sk =DbHelper.Search(Seachstudent);
                dataGridView1.DataSource=sk;
            }
                else
            {
             MessageBox.Show("抱歉，你未选择要更新的对象"，"信息提示框");
            }
        }
            else
            {
            MessageBox.Show("请重新更新","消息提示框");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var row =dataGridView1.SelectedRows.Count;
            if (row>0)
            {
             var Id=dataGridView1.SelectedRows[0].Cells["Id"].Value;
            var delect =string.Format("delete from Studentinfo where Id={0}");
                 if(res==1)
                 {
                 MessageBox.Show("删除成功"，"消息框");
                    var  Seachstudent ="select*from StudenInfo";
                      var sk =DbHelper.Search(Seachstudent);
                     dataGridView1.DataSource=sk;
                 }
                else
                 {
                  MessageBox.Show("删除失败","消息框");
                 }
            }
            else
            {
             MessageBox.Show("抱歉，您未选择要删除的对象","消息提示框");
            }
        }
    }
}
